package com.shm.client;

import java.util.ArrayList;

import com.shm.client.login.LoginActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ToggleButton;

public class SHM_ClientActivity extends Activity implements OnCheckedChangeListener, SensorEventListener{
    /** Called when the activity is first created. */
	
	public static final int SENSOR_ARRAY_SIZE = 20;
	public static final int ACCIDENT_VALUE = 39;
	
	public SHM_ClientApplication application = (SHM_ClientApplication)getApplication();
	
	private Button help_Btn;
	
	private ToggleButton shaking_Tb;
	private ToggleButton siren_Tb;
	private ToggleButton record_Tb;
	private ToggleButton police_Tb;
	
	private boolean onShaking;
	private boolean onSiren;
	private boolean onRecord;
	private boolean onPolice;

	private SensorManager sensorManager;
	private ArrayList<AccelValue> arValue;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
    	Log.d("shm", "MainActivity Start");
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        help_Btn = (Button)findViewById(R.id.help_Btn);
        help_Btn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Emergency();
			}
		});
        
        shaking_Tb = (ToggleButton)findViewById(R.id.shaking_Tb);
        shaking_Tb.setOnCheckedChangeListener(this);
        
        siren_Tb = (ToggleButton)findViewById(R.id.siren_Tb);
        siren_Tb.setOnCheckedChangeListener(this);
        
        record_Tb = (ToggleButton)findViewById(R.id.record_Tb);
        record_Tb.setOnCheckedChangeListener(this);
        
        police_Tb = (ToggleButton)findViewById(R.id.police_Tb);
        police_Tb.setOnCheckedChangeListener(this);
	        
        sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
        
        arValue = new ArrayList<AccelValue>(SENSOR_ARRAY_SIZE);
        
        if(!SHM_ClientApplication.getLoginCheck())
		{
			Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
			startActivity(intent);
		}
    }

	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		if(buttonView == shaking_Tb) {
			onShaking = isChecked;
			
			if(onShaking) {
				sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 
						SensorManager.SENSOR_DELAY_FASTEST);
			}
			else {
				sensorManager.unregisterListener(this);
			}
				
		}
		else if(buttonView == siren_Tb) {
			onSiren = isChecked;
		}
		else if(buttonView == record_Tb) {
			onRecord = isChecked;
		}
		else if(buttonView == police_Tb) {
			onPolice = isChecked;
		}
		
	}

	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub
		
	}

	public void onSensorChanged(SensorEvent event) {
		// TODO Auto-generated method stub
		float[] v = event.values;
		switch (event.sensor.getType()) {
		case Sensor.TYPE_ACCELEROMETER:
			if(arValue.size() > SENSOR_ARRAY_SIZE)
			{
				arValue.remove(0);
			}
			AccelValue av = new AccelValue();
			av.x = v[0];
			av.y = v[1];
			av.z = v[2];
			arValue.add(av);
			
			float temp = getDiffMinMax();
			if(temp > ACCIDENT_VALUE)
			{
				Emergency();
				
				Log.d("shm", "================================");
				Log.d("shm", Float.toString(temp));
				Log.d("shm", Float.toString(arValue.get(0).x));
				Log.d("shm", Float.toString(arValue.get(0).y));
				Log.d("shm", Float.toString(arValue.get(0).z));
				Log.d("shm", "================================");
			}
			
//			float temp = getDiffMinMax();
//			if(temp > max)
//			{
//				max = temp;
//				Log.d("shm", Float.toString(max));
//				
//				Log.d("shm", "================================");
//				Log.d("shm", Float.toString(arValue.get(0).x));
//				Log.d("shm", Float.toString(arValue.get(0).y));
//				Log.d("shm", Float.toString(arValue.get(0).z));
//				Log.d("shm", "================================");
//			}
			
			break;
		}
	}
	float max = 0;
	public void Emergency()
	{
		help_Btn.setText("EMERGENCY");
		if(onSiren) {
			
		}
		
		if(onRecord) {
			
		}
		
		if(onPolice) {
			
		}
		
	}
	
	public float getDiffMinMax() {
		
		float minX = Float.MAX_VALUE, minY = Float.MAX_VALUE, minZ = Float.MAX_VALUE;
		float maxX = 0, maxY = 0, maxZ = 0;
		float diffX, diffY, diffZ;
		
		for(int i = 0; i < arValue.size(); i++)
		{
			if(arValue.get(i).x < minX)
				minX = arValue.get(i).x;
			if(arValue.get(i).x > maxX)
				maxX = arValue.get(i).x;
			
			if(arValue.get(i).y < minY)
				minY = arValue.get(i).y;
			if(arValue.get(i).y > maxY)
				maxY = arValue.get(i).y;
			
			if(arValue.get(i).z < minZ)
				minZ = arValue.get(i).z;
			if(arValue.get(i).z > maxZ)
				maxZ = arValue.get(i).z;
		}
		diffX = maxX - minX;
		diffY = maxY - minY;
		diffZ = maxZ - minZ;
		
		return (diffX > diffY) ? (diffX > diffZ) ? diffX : diffZ : (diffY > diffZ) ? diffY : diffZ;
	}
	
	class AccelValue {
		float x;
		float y;
		float z;
	}
}